package com.cx.core.magazine.service.impl;

import java.io.InputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cx.core.magazine.dao.ArticleDao;
import com.cx.core.magazine.dao.ImgresourceDao;
import com.cx.core.magazine.dao.PeriodDao;
import com.cx.core.magazine.model.bean.Article;
import com.cx.core.magazine.model.bean.Period;
import com.cx.core.magazine.model.bean.form.ArticleForm;
import com.cx.core.magazine.service.MagazineManageService;
import com.cx.core.magazine.util.JSONTool;
import com.cx.core.magazine.util.MagParm;
import com.li.core.GlobalParam;
import com.li.core.RequestEnvelope;
import com.li.core.ResponseEnvelope;
import com.li.core.serviceBase.ServiceBase;

/**
 * 
 * @author li.chh
 *
 */
public class MagazineManageServiceImpl extends ServiceBase implements
		MagazineManageService {
	private PeriodDao periodDao;

	private ArticleDao articleDao;

	private ImgresourceDao imgresourceDao;

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public void setPeriodDao(PeriodDao periodDao) {
		this.periodDao = periodDao;
	}

	public void setImgresourceDao(ImgresourceDao imgresourceDao) {
		this.imgresourceDao = imgresourceDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cx.core.magazine.service.MagazineManageService#queryAllPeriods(com
	 * .li.core.RequestEnvelope)
	 */
	@Override
	public ResponseEnvelope<String, String> queryAllPeriods(
			RequestEnvelope<String, String> rqs) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cx.core.magazine.service.MagazineManageService#queryArticlesByPeriod
	 * (com.li.core.RequestEnvelope)
	 */
	@Override
	public ResponseEnvelope<String, String> queryArticlesByPeriod(
			RequestEnvelope<String, String> rqs) throws Exception {
		ResponseEnvelope<String, String> res = new ResponseEnvelope<String, String>();

		int periodId = Integer.parseInt(rqs.getParameter("periodId"));
		String loadimageFlag = (String) rqs.getParameter("loadimageflag");// 是否加载图像信息
		Period  period = this.periodDao.queryPeriodById(periodId);
		List<Article> articles = this.articleDao
				.queryAriticlesByPeriodId(periodId);
		// 如果加载图像信息
		if (loadimageFlag.equals("true")) {
			for(Article a:articles){
				if (!a.getImage_id().equals("0")){
					a.setImage_id(this.imgresourceDao.queryImage(Integer.parseInt(a.getImage_id())));
				}
			}
		}
		res.addParameter("period", JSONTool.ObjectToJSON(period));
		res.addParameter("articles", JSONTool.ObjectToJSON(articles));
		res.setAppCode(GlobalParam.APP_CODE_SUCCESS);
		return res;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cx.core.magazine.service.MagazineManageService#queryPeriods(com.li
	 * .core.RequestEnvelope)
	 */
	@Override
	public ResponseEnvelope<String, String> queryPeriods(
			RequestEnvelope<String, String> rqs) throws Exception {
		ResponseEnvelope<String, String> res = new ResponseEnvelope<String, String>();

		int year = Integer.parseInt(rqs.getParameter("year"));
		String loadimageFlag = (String) rqs.getParameter("loadimageflag");// 是否加载图像信息
		List<Period> periods = periodDao.queryPeriodByYear(year);
		// 如果加载图像信息
		if (loadimageFlag.equals("true")) {
			for(Period p:periods){
				p.setCover_id(this.imgresourceDao.queryImage(Integer.parseInt(p.getCover_id())));
			}
		}

		res.setAppCode(GlobalParam.APP_CODE_SUCCESS);
		res.addParameter("periods", JSONTool.ObjectToJSON(periods));

		return res;
	}
	
	/**
	 * 20120304 add by lichen
	 * 查询年度列表
	 */
	public ArrayList<String> queryYears() throws Exception{
		ArrayList<String> years = periodDao.queryYears();
		return years;
	}

	@Override
	public String saveArticle(Article article)
			throws Exception {
		this.articleDao.saveArticle(article);
		return null;
	}

	@Override
	public List<Period> queryPeriods(int year) throws Exception {
		List<Period> periods = periodDao.queryPeriodByYear(year);
		return periods;
	}
	
	public ResponseEnvelope<String, String> queryFrontArticlesByPeriod(
			RequestEnvelope<String, String> rqs) throws Exception {
		ResponseEnvelope<String, String> res = new ResponseEnvelope<String, String>();

		int periodId = Integer.parseInt(rqs.getParameter("periodId"));
		String loadimageFlag = (String) rqs.getParameter("loadimageflag");// 是否加载图像信息
		Period  period = this.periodDao.queryPeriodById(periodId);
		
		List<Article> articles = this.articleDao.queryAriticlesByPeriodId(periodId);
		// 如果加载图像信息
		if (loadimageFlag.equals("true")) {
			for(Article a:articles){
				if (!a.getImage_id().equals("0")){
					a.setImage_id(this.imgresourceDao.queryImage(Integer.parseInt(a.getImage_id())));
				}else{
					a.setImage_id("0_");
				}
			}
		}
		List<ArticleForm> afs = new ArrayList<ArticleForm>();
		for(Article a:articles){
			ArticleForm af = new ArticleForm();
			af.setYear(period.getYear());
			af.setYear_period(period.getYear_period());
			af.setPeriod(period.getPeriod());
			
			af.setId(a.getId());
			af.setTitle(a.getTitle());
			af.setPublisher(a.getPublisher());
			af.setType(a.getType());
			af.setImage_id(a.getImage_id());
			af.setContent(a.getContent());
			af.setPublish_date(a.getPublish_date());
			af.setUser(a.getUser());
			af.setTimestamp(a.getTimestamp());
			afs.add(af);
		}
		res.addParameter("aritcleForm", JSONTool.ObjectToJSON(afs));
		res.setAppCode(GlobalParam.APP_CODE_SUCCESS);
		return res;
	}
	
	@Override
	public String updateArticle(Article article) throws Exception {
		this.articleDao.updateArticle(article);
		return null;
	}
	
	public int saveImage(String filename,byte[] bytes) throws Exception {
		int imageid = 0;
		if ( 0 != bytes.length ){
			imageid = this.imgresourceDao.saveImage(filename, bytes);
		}
		return imageid;
	}

	@Override
	public void deleteArticle(int id) throws Exception {
		this.articleDao.deleteArticle(id);
		
	}

	@Override
	public void deleteImage(String image_id) throws Exception {
		this.imgresourceDao.deleteImage(image_id);
		
	}
}
